package com.lee.lxl.idcard.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.fuiou.pay.FyPay;
import com.fuiou.pay.activity.RequestOrder;
import com.fuiou.pay.util.AppConfig;
import com.fuiou.pay.util.MD5UtilString;
import com.lee.lxl.idcard.R;
import com.zhy.autolayout.AutoLayoutActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TestActivity extends AutoLayoutActivity {

    @Bind(R.id.button)
    Button button;
    @Bind(R.id.tv123)
    TextView tv123;

    private AbHttpUtil mAbHttpUtil = null;
    private EditText  BankCard,   IdNo, Name;
    private String MchNtCd,UserId,IdType,  MchntOrdId,orderNo, Amt;
    private Button pay;
    private String mMchnt_Key = "5old71wihg2tqjug9kkpxnhx9hiujoqj";
    private String mURL = "http://www.ligoujun.site/Api/pay/payReapalNotice";
    public RequestOrder requestOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);

        mAbHttpUtil = AbHttpUtil.getInstance(this);
        mAbHttpUtil.setTimeout(10000);
        mAbHttpUtil.setEncode("UTF-8");

        initView();
        FyPay.init(TestActivity.this);

    }

    private void initView() {
        requestOrder = new RequestOrder(this);
        MchNtCd = "0002900F0096235";
        UserId = "3";
        BankCard = (EditText) findViewById(R.id.BankCard);
        Amt = "500";
        IdType = "0";
        IdNo = (EditText) findViewById(R.id.IdNo);
        Name = (EditText) findViewById(R.id.Name);

    }

    /**
     * 返回订单相关信息(手动退出)
     */
    @Override
    protected void onStart() {
        /**
         * 请求发送成功的返回数据 发起支付
         */
        Log.e("zls", "发送成功请求的返回数据：" + AppConfig.getData(TestActivity.this, AppConfig.RSP_SDK_DATA));

        if(AppConfig.getData(TestActivity.this, AppConfig.RSP_SDK_DATA).length()>1){
            String [] string1 = AppConfig.getData(TestActivity.this, AppConfig.RSP_SDK_DATA).split("<RESPONSEMSG>");
            String string2=string1[1];
            String [] string3 = string2.split("</RESPONSEMSG>");
            Toast.makeText(this, string3[0], Toast.LENGTH_SHORT).show();
        }

        reset();
        super.onStart();
    }
    /**
     * 清除数据
     */
    private void reset() {
        AppConfig.setData(TestActivity.this, AppConfig.RSP_CODE, "");
        AppConfig.setData(TestActivity.this, AppConfig.RSP_DESC, "");
        AppConfig.setData(TestActivity.this, AppConfig.RSP_SDK_DATA, "");
    }

    @OnClick({R.id.button, R.id.pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                // 绑定参数
                AbRequestParams params = new AbRequestParams();
                params.put("uid", "3");
                params.put("token", "31ECB256343AE7CC450E39C28EA4D3C5C61D862C48DB739A92DB50C828FD3BF5");
                params.put("money", "1000000");
                mAbHttpUtil.get("http://www.ligoujun.site/Api/pay/payOrder", params, new AbStringHttpResponseListener() {
                    @Override
                    public void onSuccess(int i, String s) {
                        Toast.makeText(TestActivity.this, s, Toast.LENGTH_SHORT).show();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.getString("code").equals("200")) {
                                JSONObject object = jsonObject.getJSONObject("data");
                                Log.e("TestActivity", object.getString("pay_id"));
                                tv123.setText(object.getString("pay_id"));
                                MchntOrdId = object.getString("pay_id");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onStart() {
                        Toast.makeText(TestActivity.this, "start", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFinish() {
                        Toast.makeText(TestActivity.this, "finish", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(int i, String s, Throwable throwable) {
                        Log.e("TestActivity",i + "      " + s  +"     "+ throwable);
                    }
                });
                break;
            case R.id.pay:

                String Sing = MD5UtilString.MD5Encode(
                        "02" + "|" + "2.0" + "|" + MchNtCd + "|" + MchntOrdId
                                + "|" + UserId + "|" + Amt + "|"
                                + BankCard.getText().toString() + "|" + mURL + "|" + Name.getText().toString() + "|"
                                + IdNo.getText().toString() + "|" + IdType + "|" + mMchnt_Key);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_CD, MchNtCd);
                Log.e("zls", MchNtCd);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_AMT, Amt);
                Log.e("zls", MchNtCd);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_BACK_URL, mURL);
                Log.e("zls", mURL);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_BANK_NUMBER, BankCard.getText().toString());
                Log.e("zls", BankCard.getText().toString());
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_ORDER_ID, MchntOrdId);
                Log.e("zls", MchntOrdId);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_USER_IDCARD_TYPE, IdType);
                Log.e("zls", IdType);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_USER_ID, UserId);
                Log.e("zls", UserId);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_USER_IDNU, IdNo.getText().toString());
                Log.e("zls", IdNo.getText().toString());
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_USER_NAME, Name.getText().toString());
                Log.e("zls", Name.getText().toString());
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_SING_KEY, Sing);
                Log.e("zls", Sing);
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_SDK_SIGNTP, "MD5");
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_SDK_TYPE, "02");
                AppConfig.setData(TestActivity.this, AppConfig.MCHNT_SDK_VERSION, "2.0");

                requestOrder.Request();

                break;
        }
    }
}
